﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Production_Set.API.MaterialManagement.Add;
using Production_Set.API.MaterialManagement.Delete;
using Production_Set.API.MaterialManagement.Export;
using Production_Set.API.MaterialManagement.State;
using Production_Set.API.MaterialManagement.Update;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MaterialController : ControllerBase
    {
        public MaterialController(IMediator mediator, IWebHostEnvironment webHostEnvironment)
        {
            this.mediator = mediator;
            this.webHostEnvironment = webHostEnvironment;
        }

        /// <summary>
        /// 中介者
        /// </summary>
        public IMediator mediator { get; set; }
        public IWebHostEnvironment webHostEnvironment { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>

        /// <summary>
        /// 添加物料信息
        /// </summary>
        /// <param name="Material"></param>
        /// <returns></returns>
        [HttpPost("AddMaterial")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> AddMaterial(MaterialCreateCommon Material)
        {
            return await mediator.Send(Material);
        }
        /// <summary>
        /// 添加物料清单
        /// </summary>
        /// <param name="Material"></param>
        /// <returns></returns>
        [HttpPost("AddBillMaterial")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> AddBillMaterial(BillMaterialCreateCommon Material)
        {
            return await mediator.Send(Material);
        }
        /// <summary>
        /// 添加物料需求计划
        /// </summary>
        /// <param name="Material"></param>
        /// <returns></returns>
        [HttpPost("AddMaterialPlanning")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> AddMaterialPlanning(MaterialPlanningCreateCommon Material)
        {
            return await mediator.Send(Material);
        }
        /// <summary>
        /// 修改物料需求计划
        /// </summary>
        /// <param name="Material"></param>
        /// <returns></returns>
        [HttpPost("UpdateMaterialPlanning")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> UpdateMaterialPlanning(MaterialPlanningUpdateCommon Material)
        {
            return await mediator.Send(Material);
        }
        /// <summary>
        /// 逻辑删除物料需求计划
        /// </summary>
        /// <param name="Material"></param>
        /// <returns></returns>
        [HttpPost("DeleteMaterialPlanning")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> DeleteMaterialPlanning(MaterialPlanningDeleteCommon Material)
        {
            return await mediator.Send(Material);
        }
        /// <summary>
        /// 导入物料需求计划
        /// </summary>
        /// <param name="Material"></param>
        /// <returns></returns>
        [HttpPost("ExportMaterialPlanning")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> ExportMaterialPlanning(MaterialPlanningExportCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 导入Excel物料需求计划
        /// </summary>
        /// <param name="Material"></param>
        /// <returns></returns>
        [HttpPost("AddExcelData")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> AddExcelData(IFormFile file)
        {
            var rootpath = webHostEnvironment.WebRootPath+"/Excel/";
            var fileName=Guid.NewGuid().ToString()+file.FileName;
            using(FileStream fs = System.IO.File.Create(rootpath + fileName))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            //实例化
            MaterialPlanningExportCommon addMaterialPlanningExportCommon = new MaterialPlanningExportCommon()
            {
                ExportName = fileName,
            };
            return await ExportMaterialPlanning(addMaterialPlanningExportCommon);
        }
        /// <summary>
        /// 添加物料清单行
        /// </summary>
        /// <param name="Tasks"></param>
        /// <returns></returns>
        [HttpPost("AddRowBillMaterial")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> AddRowBillMaterial(RowBillMaterialCreateCommon RowBillMaterial)
        {
            return await mediator.Send(RowBillMaterial);
        }
        /// <summary>
        /// 添加物料需求行
        /// </summary>
        /// <param name="Tasks"></param>
        /// <returns></returns>
        [HttpPost("AddRowMaterialPlanning")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> AddRowMaterialPlanning(RowMaterialPlanningCreateCommon RowMaterialPlanning)
        {
            return await mediator.Send(RowMaterialPlanning);
        }
        /// <summary>
        /// 添加生产任务
        /// </summary>
        /// <param name="Tasks"></param>
        /// <returns></returns>
        [HttpPost("AddTasks")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> AddTasks(TasksCreateCommon Tasks)
        {
            return await mediator.Send(Tasks);
        }
        /// <summary>
        /// 修改生产任务
        /// </summary>
        /// <param name="Tasks"></param>
        /// <returns></returns>
        [HttpPost("UpdateTasks")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> UpdateTasks(TasksUpdateCommon Tasks)
        {
            return await mediator.Send(Tasks);
        }
        /// <summary>
        ///审批
        /// </summary>
        /// <param name="Tasks"></param>
        /// <returns></returns>
        [HttpPost("UpdaState")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> UpdaState(TaskStateCommend Tasks)
        {
            try
            {
                return await mediator.Send(Tasks);
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="Tasks"></param>
        /// <returns></returns>
        [HttpPost("UpdaTaskSubmit")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> UpdaTaskSubmit(TaskSubmitCommend Tasks)
        {
            try
            {
                return await mediator.Send(Tasks);
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        ///驳回
        /// </summary>
        /// <param name="Tasks"></param>
        /// <returns></returns>
        [HttpPost("UpdaTaskOverrule")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> UpdaTaskOverrule(TaskOverruleCommend Tasks)
        {
            try
            {
                return await mediator.Send(Tasks);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        ///出库单添加
        /// </summary>
        /// <param name="Tasks"></param>
        /// <returns></returns>
        [HttpPost("AddOutBund")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> AddOutBund(OutBundCreateCommon Tasks)
        {
            try
            {
                return await mediator.Send(Tasks);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        ///出库单修改
        /// </summary>
        /// <param name="Tasks"></param>
        /// <returns></returns>
        [HttpPost("OutBoundUpdate")]
        public async Task<THE_WIND_RISES.Common.Dtos.Result> OutBoundUpdate(OutBoundUpdateCommon Tasks)
        {
            try
            {
                return await mediator.Send(Tasks);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
